package com.tanhua.sso.server.controller;

import com.tanhua.result.Result;
import com.tanhua.sso.pojo.UserInfo;
import com.tanhua.sso.server.service.UsersService;
import com.tanhua.sso.vo.SettingsVo;
import com.tanhua.sso.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    /*获取个人资料*/
    @GetMapping
    public ResponseEntity users(Long userID){
        UserInfoVo userInfoVo=usersService.users(userID);
        return ResponseEntity.status(200).body(userInfoVo);
    }

    /*修改个人资料*/
    @PutMapping
    public ResponseEntity update(@RequestBody UserInfo userInfo){
        usersService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /*互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
    * type:   1 互相喜欢
              2 我喜欢
              3 粉丝
              4 谁看过我*/
    @GetMapping("/friends/{type}")
    public ResponseEntity type(@PathVariable Integer type,@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pagesize,String nickname){
        Result result=usersService.type(type,page,pagesize,nickname);
        return ResponseEntity.ok(result);
    }


    /*互相喜欢，喜欢，粉丝 - 统计*/
    @GetMapping("counts")
    public ResponseEntity counts(){
        Map map=usersService.count();
        return ResponseEntity.ok(map);
    }

    //发送手机验证码
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        usersService.sendVerificationCode();
        return ResponseEntity.ok(null);
    }

    /*校验验证码*/
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        String verificationCode = (String) map.get("verificationCode");
        boolean flag=usersService.checkVerificationCode(verificationCode);
        Map map1=new HashMap();
        map1.put("verification",flag);
        return ResponseEntity.ok(map1);
    }

    /*修改手机号*/
    @PostMapping("phone")
    public ResponseEntity setPhone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        usersService.setPhone(phone);
        return ResponseEntity.ok(null);
    }

    /*粉丝喜欢*/
    @PostMapping("fans/{uid}")
    public ResponseEntity fans(@PathVariable Long uid){
        usersService.fans(uid);
        return ResponseEntity.ok(null);
    }

    /*取消喜欢*/
    @DeleteMapping("like/{uid}")
    public ResponseEntity unLike(@PathVariable Long uid){
        usersService.unLike(uid);
        return ResponseEntity.ok(null);
    }

    /*更新头像*/
    @PostMapping("header")
    public ResponseEntity updateHeader(MultipartFile headPhoto) throws IOException {
        usersService.updateHeader(headPhoto);
        return ResponseEntity.ok(null);
    }

    /*通用户用设置  读取*/
    @GetMapping("settings")
    public ResponseEntity settings(){
        SettingsVo settings=usersService.settings();
        return ResponseEntity.ok(settings);
    }

    /*设置陌生人问题保存*/
    @PostMapping("questions")
    public ResponseEntity questions(@RequestBody Map map){
        String content = (String) map.get("content");
        usersService.questions(content);
        return ResponseEntity.ok(null);
    }

    /*通知设置保存*/
    @PostMapping("notifications/setting")
    public ResponseEntity setting(@RequestBody Map map){
        Boolean likeNotification = (Boolean) map.get("likeNotification");
        Boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        Boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");
        usersService.setting(likeNotification,pinglunNotification,gonggaoNotification);
        return ResponseEntity.ok(null);
    }

    /*黑名单列表*/
    @GetMapping("blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){

       Result result= usersService.blacklist(page,pagesize);
       return ResponseEntity.ok(result);
    }

    /*黑名单移除*/
    @DeleteMapping("blacklist/{uid}")
    public ResponseEntity delectBlackList(@PathVariable Long uid){
        usersService.delectBlackList(uid);
        return ResponseEntity.ok(null);
    }

    /*是否喜欢*/
    @GetMapping("/{uid}/alreadyLove")
    public ResponseEntity alreadyLove(@PathVariable Long uid){
        boolean flag=usersService.alreadyLove(uid);
        return ResponseEntity.ok(flag);
    }
}
